package leetcode;

public class Lc917 {

    public String reverseOnlyLetters(String s) {
        int i = 0, j = s.length() - 1;
        char[] chars = s.toCharArray();
        while (i < j) {
            while (i < j && !Character.isLetter(chars[i])) i++;
            while (i < j && !Character.isLetter(chars[j])) j--;
            if (i < j) {
                char temp = chars[i];
                chars[i] = chars[j];
                chars[j] = temp;
                i++;
                j--;
            }
        }
        return new String(chars);
    }

}
